package com.appkudos.database;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
//import android.widget.LinearLayout;
import android.widget.TextView;
	//import android.widget.Toast;

public class AppKudos_database extends ListActivity {
    /** Called when the activity is first created. */
	public static final String KEY_121 = "http://www.tiffany-ng.com/appkudos/php-test.php?";
	public static final String KEY_122 = "http://www.tiffany-ng.com/appkudos/GroupControllerQueries.php?";

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView txt = (TextView) this.findViewById(R.id.hello);
	    
	    /*An example of retrieve elements of the database and 
	      put them into a ListView. This example in particular
	      gets the list of users and shows their usernames.*/ 
	    //setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.getServerData(KEY_121)));
	    
	    /*An example of insert a value in the database. This example
	      in particular, insert a new category.
	     */
	    txt.setText(getServerData1("New Category from app"));
	     
    }
    
    
    
     public ArrayList<String> getServerData(String returnString) {
    	ArrayList<String> listItems = new ArrayList<String>();
    	InputStream is = null;
 	    try{
 	            HttpClient httpclient = new DefaultHttpClient();          
 	            HttpPost httppost = new HttpPost(KEY_121);            
 	            HttpResponse response = httpclient.execute(httppost);	           
 	            HttpEntity entity = response.getEntity();
 	            is = entity.getContent();	          
 	
 	    }catch(Exception e){
 	            Log.e("log_tag", "Error in http connection "+e.toString());
 	    }
 		
 	    //convert response to string
 	    try{
 	    	BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            String line = null;
            while ((line = reader.readLine()) != null) {
                    JSONArray jArray2 = new JSONArray(line);
                    for(int i=0;i<jArray2.length();i++){
                    	listItems.add(jArray2.getJSONObject(i).getString("username"));
                    }
            }
            is.close();
 	    }catch(Exception e){
 	    	listItems.add(e.toString());
 	            Log.e("log_tag", "Error converting result "+e.toString());
 	    }
 	     	    
 	    return listItems;
	}
     
     public String getServerData1(String name) {
 	String confirmation = "";
 	InputStream is = null;
 	ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     nameValuePairs.add(new BasicNameValuePair("function","createCategory"));
     nameValuePairs.add(new BasicNameValuePair("name",name));
     
	    try{
	            HttpClient httpclient = new DefaultHttpClient();          
	            HttpPost httppost = new HttpPost(KEY_122);
	            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	            HttpResponse response = httpclient.execute(httppost);	           
	            HttpEntity entity = response.getEntity();
	            is = entity.getContent();	          
	
	    }catch(Exception e){
	    		confirmation += e.toString();
	            Log.e("log_tag", "Error in http connection "+e.toString());
	    }
		
	    //convert response to string
	    try{
	    	BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
         String line = null;
         line = reader.readLine();
         JSONArray jArray2 = new JSONArray("["+line+"]");
         if (jArray2.getJSONObject(0).has("ERROR")){
         	confirmation = jArray2.getJSONObject(0).getString("ERROR");
         }else{
         	if (jArray2.getJSONObject(0).has("SUCCESSFUL")){
         		confirmation = "SUCCESSFUL";
         	}
         	else
         		confirmation = "NOTHING HAPPENDED";
         }
         is.close();
	    }catch(Exception e){
	    	confirmation += e.toString();
	    	Log.e("log_tag", "Error converting result "+e.toString());
	    }
	     	    
	    return confirmation;
	}
     
}